<template>
	<view>
		<input type="text" v-model="keyword" />
		<button type="default" @click="search">搜索</button>
		
		<view class="history" v-show="list.length==0&&history.length!=0">
			<uni-tag v-for="(item,index) in history" :inverted="true" :text="item" type="primary" />
		</view>
		
		<view class="con">
			<view v-for="(item,index) in list" class="" @click="gotoDetail(item.goods_id)">
				{{item.goods_name}}
			</view>
		</view>
		
	</view>
</template>

<script>
	import {getList} from "../../request/api.js"
	export default {
		data() {
			return {
				keyword:"",
				history:uni.getStorageSync("history")||[],
				list:[]
			}
		},
		methods: {
			search(){
				let index=this.history.indexOf(this.keyword)
				
				console.log(index);
				if(index!=-1){
					this.history.splice(index,1)
				}
				
				this.history.splice(4,1)
				this.history.unshift(this.keyword)
				// 去重加上最多存五条
				
				uni.setStorageSync("history",this.history)
				// this.keyword=""
				
				getList({query:this.keyword}).then(res=>{
					console.log(res);
					this.list=res.message.goods
				})
			},
			gotoDetail(id){
				uni.navigateTo({
					url:"/pages/detail/detail?goods_id="+id
				})
				
			}
			
		}
	}
</script>

<style lang="scss">
	.con{
		view{
			height: 80rpx;
			padding: 16rpx;
		}
	}

</style>
